/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.eit.ntc.lmsbackend.struts.action;

import com.eit.ntc.lmsbackend.utils.RoleUtils;
import com.eit.toschoolonline.lms.jdo.DocumentItemM;
import com.eit.toschoolonline.lms.jdo.LearningItemM;
import com.eit.toschoolonline.lms.jdo.SubjectM;
import com.eit.toschoolonline.lms.service.LmsServiceFactory;
import com.eit.toschoolonline.lms.service.SubjectService;
import com.eit.toschoolonline.lms.service.XmlFileService;
import com.eit.toschoolonline.lms.service.ZipItemService;
import com.eit.toschoolonline.lms.service.spring.ZipItemServiceSpringBean;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.zip.ZipOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;

/**
 *
 * @author chanon
 */
public class ZipAction extends DownloadAction {

    private static String enType = "2";
    private static String eallType = "1";
    private static String exType = "3";
    private ZipItemService zipItemService;
    private final static String SUCCESS = "success";
    private XmlFileService xmlFileServiceSpringBean;
    //private SubjectService subjectServiceSpringBean;

//    public SubjectService getSubjectServiceSpringBean() {
//        return subjectServiceSpringBean;
//    }
//
//    public void setSubjectServiceSpringBean(SubjectService subjectServiceSpringBean) {
//        this.subjectServiceSpringBean = subjectServiceSpringBean;
//    }

    public XmlFileService getXmlFileServiceSpringBean() {
        return xmlFileServiceSpringBean;
    }

    public void setXmlFileServiceSpringBean(XmlFileService xmlFileServiceSpringBean) {
        this.xmlFileServiceSpringBean = xmlFileServiceSpringBean;
    }
//    static String path ="c:/TestZip/";
    private LmsServiceFactory lmsServiceFactory;

    public LmsServiceFactory getLmsServiceFactory() {
        return lmsServiceFactory;
    }

    public void setLmsServiceFactory(LmsServiceFactory lmsServiceFactory) {
        this.lmsServiceFactory = lmsServiceFactory;
    }

    @Override
    protected StreamInfo getStreamInfo(ActionMapping arg0, ActionForm arg1, HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
        String type = arg2.getParameter("type");
        String subjectCode = arg2.getParameter("selectSubject");
        //ZipItemService zservice = getZipItemService();
        List zipItems = (List) arg2.getSession().getAttribute("sumZipItem");
        ByteArrayOutputStream bStream = new ByteArrayOutputStream();
        ZipOutputStream zos = new ZipOutputStream(bStream);

        if (subjectCode==null || subjectCode.trim().equals("")){
           List subjectList = lmsServiceFactory.getSubjectService().getSubjectAll();
           Iterator iter = subjectList.iterator();
           while(iter.hasNext()){
             SubjectM subject =  (SubjectM) iter.next();
             getZip(type,subject.getSubjectCode(),zipItems,RoleUtils.getRole(arg2),zos);
           }
        }else {
            getZip(type,subjectCode,zipItems,RoleUtils.getRole(arg2),zos);
        };
              
//        String filePath = path+type+".zip";
//        File file = new File(filePath);
//
//        return new FileStreamInfo(contentType, file);

         String contentType = "application/x-zip-compressed";

        arg3.setContentType("application/x-zip-compressed");
        arg3.setHeader("Content-disposition", "attachment;filename=update.zip" );
        zos.close();
        byte[] datas = bStream.toByteArray();
        return new ByteArrayStreamInfo(contentType, datas);
    }

//    private String getRoleZip(HttpServletRequest request){
//        String role = "";
//        if (request.isUserInRole("NTC_ADMIN")){
//            role = DocumentItemM.NTC_ROLE;
//        };
//        if (request.isUserInRole("INFO_ADMIN")){
//            role = DocumentItemM.INFO_ROLE;
//        };
//        return role;
//    }
     public ZipItemService getZipItemService() {
        return zipItemService;
    }

    public void setZipItemService(ZipItemService zipItemService) {
        this.zipItemService = zipItemService;
    }

    private void getZip(String itemType,String subjectCode,List zipItems,String role,ZipOutputStream zos) {
        System.out.println("get by role:"+role);
        ZipItemService zservice = getZipItemService();
        //List otherItems = new ArrayList();
        byte[] xmlOtherDataByte = null;
        byte[] xmlEnDataByte = null;
        byte[] xmlEnOtherDateByte = null;
        //if (itemType!=null && itemType.trim().equals(ZipItemServiceSpringBean.EDOCUMENT_XML_TYPE)){
           //otherItems = lmsServiceFactory.getDocumentItemService().getDocBySubject(subjectCode,role);
           List xmlOtherItems = lmsServiceFactory.getDocumentItemService().getDocBySubject(subjectCode);
           xmlOtherDataByte = xmlFileServiceSpringBean.createDocItemXMLByte(subjectCode,xmlOtherItems);
       // } else if (itemType!=null && itemType.trim().equals(ZipItemServiceSpringBean.EENTRANCE_XML_TYPE)){
            //items = lmsServiceFactory.getEntranceItemService().getEnBySubject(subjectCode,role);
            List xmlEnItems = lmsServiceFactory.getEntranceItemService().getEnBySubject(subjectCode);
            xmlEnDataByte = xmlFileServiceSpringBean.createEnItemXMLByte(subjectCode, xmlEnItems);
            xmlEnOtherDateByte = xmlFileServiceSpringBean.createEnOtherItemXMLByte(subjectCode, xmlEnItems);
       // }
       // List items = lmsServiceFactory.getLearningItemService().loadLearningItemByType(LearningItemM.ECOURSE_TYPE);
        zservice.createZipStream(zos, itemType, zipItems);
//        if (itemType!=null && itemType.trim().equals(ZipItemServiceSpringBean.EDOCUMENT_XML_TYPE)){
//           items = lmsServiceFactory.getDocumentItemService().getDocBySubject(subjectCode,role);
//        } else if (itemType!=null && itemType.trim().equals(ZipItemServiceSpringBean.EENTRANCE_XML_TYPE)){
//            items = lmsServiceFactory.getEntranceItemService().getEnBySubject(subjectCode,role);
//        }

        if (itemType!=null &&  (itemType.trim().equals(eallType) || itemType.trim().equals(exType) )){
            zservice.addZipByte(zos, xmlOtherDataByte, ZipItemServiceSpringBean.EDOCUMENT_XML_TYPE, subjectCode);
        };
        if (itemType!=null &&  (itemType.trim().equals(enType) || itemType.trim().equals(exType) )){
            zservice.addZipByte(zos, xmlEnDataByte, ZipItemServiceSpringBean.EENTRANCE_XML_TYPE, subjectCode);
            zservice.addZipByte(zos, xmlEnOtherDateByte, ZipItemServiceSpringBean.EENTRANCE_OTHER_XML_TYPE, subjectCode);
        };
    }

//    private String[] getListSubjectCode() {
//        List subjectList = subjectServiceSpringBean.getSubjectAll();
//        List sCodeList = new ArrayList();
//        Iterator iter  = subjectList.iterator();
//        while(){
//
//        }
//    }

     protected class ByteArrayStreamInfo implements StreamInfo {

        protected String contentType;
        protected byte[] bytes;

        public ByteArrayStreamInfo(String contentType, byte[] bytes) {
            this.contentType = contentType;
            this.bytes = bytes;
        }

        public String getContentType() {
            return contentType;
        }

//        @Override
        public InputStream getInputStream() throws IOException {
            return new ByteArrayInputStream(bytes);
        }


    }
}
